/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
  * author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.seeyon.apps.ext.kingdee.dao;


import com.seeyon.apps.ext.kingdee.util.StringUtil;

import java.io.Reader;
import java.io.StringWriter;
import java.sql.*;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * @author chenYuan (mail:39793751@qq.com)
 * date: 2007-1-10
 * Time: 11:16:41
 * Oracle 数据库 SQL 匹配
 */
public class OracleDialect  {



    /**
     * @param rs    返回对象
     * @param index 索引
     * @return 返回查询结果
     * @throws SQLException 异常
     */

    public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
        String typeName = rs.getMetaData().getColumnTypeName(index).toLowerCase();
        int colSize = rs.getMetaData().getColumnDisplaySize(index);
        ///////大数值
        if ("ROWID".equals(typeName)) {
            return rs.getString(index);
        }

        //短断整型
        if (("int".equals(typeName) && colSize < 4) || "short".equals(typeName) || "smallint".equals(typeName) || "int2".equals(typeName) || "tinyint".equals(typeName) || ("fixed".equals(typeName) && colSize < 4)) {
            return rs.getShort(index);
        }
        //////////整型
        if ("int".equals(typeName) || "integer".equals(typeName) || "int4".equals(typeName) || ("number".equals(typeName) && colSize < 8) || ("fixed".equals(typeName) && colSize < 19)) {
            return rs.getInt(index);
        }

        ///////长整型
        if ("number".equals(typeName) || "bigint".equals(typeName) || "int8".equals(typeName) || ("fixed".equals(typeName))) {
            return rs.getLong(index);
        }

        ///////单精度
        if ("money".equals(typeName) || "float".equals(typeName) || "real".equals(typeName) || "binary_float".equals(typeName)) {
            return rs.getFloat(index);
        }
        ///////大数值
        if ("decimal".equals(typeName)) {
            return rs.getBigDecimal(index);
        }
        ///////双精度
        if ("double".equals(typeName) || "double precision".equals(typeName) || "binary_double".equals(typeName)) {
            return rs.getDouble(index);
        }

        ///////日期
        if ("date".equals(typeName)) {
            java.sql.Date t = rs.getDate(index);
            if (t == null) {
                return null;
            }
            return new Date(t.getTime());
        }

        ///////日期时间
        if (typeName.contains("timestamp") || "datetime".equals(typeName)) {
            Timestamp t = rs.getTimestamp(index);
            if (t == null) {
                return null;
            }
            return new Date(t.getTime());
        }


        ////////////时间
        if ("time".equals(typeName)) {
            return rs.getTime(index);
        }

        ///////短字符串
        if ("char".equals(typeName) || "nvarchar".equals(typeName) || "varchar".equals(typeName)|| "long".equals(typeName) || "varchar2".equals(typeName) || "tinyblob".equals(typeName)) {
            return rs.getString(index);
        }

        ////////////大文本类型
        if ("CLOB".equals(typeName) || "mediumtext".equals(typeName) || " long varchar".equals(typeName)
                || "ntext".equals(typeName) || "text".equals(typeName) || "long raw".equals(typeName)) {
            //oracle.sql.CLOB clob = (oracle.sql.CLOB) rs.getClob(index);
            Clob clob = rs.getClob(index);
            if (clob == null) {
                return StringUtil.empty;
            }
            Reader bodyReader = clob.getCharacterStream();

            StringWriter out = new StringWriter(255);
            try {
                char[] buf = new char[256];
                int i;
                while ((i = bodyReader.read(buf)) != -1) {
                    out.write(buf, 0, i);
                }
                out.close();
                bodyReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return out.toString();
        }

        ///////二进制类型 文件类型
        if ("image".equals(typeName) || "blob".equals(typeName) || "mediumblob".equals(typeName)
                || "longblob".equals(typeName) || "dbclob".equals(typeName)
                || "long byte".equals(typeName)
                || "varbinary".equals(typeName) || "binary".equals(typeName)) {
            // oracle.sql.BLOB blob = (oracle.sql.BLOB) rs.getBlob(index);
            // Clob clob = rs.getClob(index);
            // return clob.getBinaryStream();
            return rs.getAsciiStream(index);
        }
        return rs.getObject(index);
    }



}